using System;
using System.Collections;
using DG.Tweening;
using Unity.VisualScripting;
using UnityEngine;
using Random = UnityEngine.Random;

namespace Script.SteerBehaviour
{
    public class WanderBehavior : MonoBehaviour
    {
        public Vector2 targetChangeRange;
        public float wanderRadius = 2;

        private Vector2 targetPos;
        private SteerBehaviour steerBehaviour;
        private Rigidbody2D rb;

        private void Awake()
        {
            steerBehaviour = GetComponent<SteerBehaviour>();
            rb = GetComponent<Rigidbody2D>();
        }

        private void Start()
        {
            StartCoroutine(TargetPosChange());
        }

        IEnumerator TargetPosChange()
        {
            while (true)
            {
                float theta = Random.value * 2 * Mathf.PI;
                Vector3 wanderTarger = new Vector3(Mathf.Sin(theta), Mathf.Sin(theta),0);
                wanderTarger.Normalize();
                wanderTarger *= wanderRadius;
                targetPos = transform.position + wanderTarger;
                yield return new WaitForSeconds(Random.Range(targetChangeRange.x, targetChangeRange.y));
            }
        }

        public Vector3 GetSteer()
        {
            Debug.DrawLine(transform.position,targetPos,Color.gray);

            return steerBehaviour.Seek(targetPos);
        }
    }
}